package com.skp.shaphan.ui.connectioncontrols;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.Properties;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.skp.shaphan.actions.DerbyConnectAction;
import com.skp.shaphan.ui.ActionBasedPanel;
import com.skp.shaphan.ui.ConnectRunnable;
import com.skp.shaphan.ui.DatabasePathSelector;

/**
 * Connection Options GUI for an Apache Derby database connection.
 * @author Stephen Phillips
 *
 */
public class DerbyConnectionOptions extends JPanel implements ActionBasedPanel, ConnectionOptions {
	/**
	 * 
	 */
	private static final long serialVersionUID = -1712275548267314221L;
	private DatabasePathSelector path;
	private JCheckBox createDatabase;
	private JCheckBox buildPSTesting;
	private DerbyConnectAction action;
	
	public DerbyConnectionOptions() {
		this.setLayout(new GridBagLayout());
		JLabel label;
		int row = 0;
		
		label = new JLabel("Path to Database: ");
		this.add(label, new GridBagConstraints(0, row, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(5, 5, 5, 5), 5, 5));
		path = new DatabasePathSelector();
		this.add(path, new GridBagConstraints(1, row++, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 5, 5));
		createDatabase = new JCheckBox("Create the Database");
		createDatabase.setSelected(false);
		this.add(createDatabase, new GridBagConstraints(1, row++, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 5, 5));
		buildPSTesting = new JCheckBox("Build PS Testing Database");
		buildPSTesting.setSelected(false);
		this.add(buildPSTesting, new GridBagConstraints(1, row++, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 5, 5));
		this.add(new JLabel(), new GridBagConstraints(1, row++, 1, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(5, 5, 5, 5), 5, 5));
	}
	public String getDBPath() {
		return path.getPath();
	}
	public boolean createDatabase() {
		return createDatabase.isSelected();
	}
	public boolean buildPSTesting() {
		return buildPSTesting.isSelected();
	}
	@Override
	public ConnectRunnable getAction() {
		if(action == null) {
			action = new DerbyConnectAction(this);
		}
		return action;
	}
	@Override
	public void setConnectionOptions(Properties properties) {
		path.setPath(properties.getProperty("databasePath"));
		if(properties.getProperty("create", "no").equalsIgnoreCase("yes")) {
			createDatabase.setSelected(true);
		} else {
			createDatabase.setSelected(false);
		}
		if(properties.getProperty("psTesting", "no").equalsIgnoreCase("yes")) {
			buildPSTesting.setSelected(true);
		} else {
			buildPSTesting.setSelected(false);
		}
	}
	@Override
	public Properties getConnectionOptions() {
		Properties props = new Properties();
		props.setProperty("databasePath", path.getPath());
		if(createDatabase.isSelected()) {
			props.setProperty("create", "yes");
		} else {
			props.setProperty("create", "no");
		}
		if(buildPSTesting.isSelected()) {
			props.setProperty("psTesting", "yes");
		} else {
			props.setProperty("psTesting", "no");
		}
		return props;
	}
	@Override
	public String getUserFriendlyName() {
		return "Derby";
	}

}
